﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VirtualPress.Infrastructure.Entities.Repository;
using VirtualPress.Infrastructure.Entities.Enum;
using VirtualPress.Infrastructure.Entities.Cache;

namespace VirtualPress.Infrastructure.Entities.Entity
{
    public class User : IAccessible<T_User,User>, ILogicEntity<T_User,User>
    {
        #region
        public string UserName { get; set; }
        public string NickName { get; set; }
        public string Password { get; set; }
        public Gender Gender { get; set; }
        public DateTime Birthday { get; set; }
        /// <summary>
        /// 省份
        /// </summary>
        public AreaCode Area { get; set; }
        /// <summary>
        /// 城市
        /// </summary>
        public Location Location { get; set; }
        public Profession Profession { get; set; }
        public string QQ { get; set; }
        public string Phone { get; set; }
        public string Photo { get; set; }
        public string Email { get; set; }
        public DateTime RcdDate { get; set; }
        public bool IsRefuse { get; set; }
        public bool IsActivate { get; set; }
        public string Token { get; set; }
        public UserRole SystemRole { get; set; }
        #endregion

        /// <summary>
        /// 专家
        /// </summary>
        public Expert expert { get; set; }

        public void LoadExpertInfo()
        {
            expert = new Expert().Find(UserName);
        }

        protected override void Covariant(T_User model)
        {
            this.Area = (AreaCode)model.Area;
            this.Birthday = model.Birthday;
            this.Email = model.Email;
            this.Gender = model.Gender?Enum.Gender.Male: Enum.Gender.Female;
            this.IsActivate = model.IsActivate;
            this.IsRefuse = model.IsRefuse;
            this.Location = (Enum.Location) model.Location;
            this.NickName = model.NickName;
            this.Password = model.Password;
            this.Phone = model.Phone;
            this.Photo = model.Photo;
            this.Profession = (Enum.Profession)model.Profession;
            this.QQ = model.QQ;
            this.RcdDate = model.RcdDate;
            this.Token = model.Token;
            this.UserName = model.UserName;
            this.SystemRole = (UserRole)((int)model.SystemRole);
        }
        protected override T_User Inverter(User model)
        {
            return new T_User()
            {
                Area = (int)model.Area,
                Birthday = model.Birthday,
                Email = model.Email,
                Gender = model.Gender == Enum.Gender.Male,
                IsActivate = model.IsActivate,
                IsRefuse = model.IsRefuse,
                Location = (int)model.Location,
                NickName = model.NickName,
                Password = model.Password,
                Phone = model.Phone,
                Photo = model.Photo,
                Profession = (int)model.Profession,
                QQ = model.QQ,
                RcdDate = model.RcdDate,
                Token = model.Token,
                UserName = model.UserName,
                SystemRole = (int)model.SystemRole
            };
        }
        public User Set(T_User dbModel)
        {
            Covariant(dbModel);
            return this;
        }

        /// <summary>
        /// 将启用缓存,通过username查找
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public override User Find(string key)
        {
            return new UserCache().GetItem(key);
        }
        public override User PostDelete()
        {
            new UserCache().RefreshItem(UserName);
            return base.PostDelete();
        }
        public override User PostModify()
        {
            new UserCache().RefreshItem(UserName);
            return base.PostModify();
        }
    }
}
